package cn.sofwin.web.blog.dao;

import cn.sofwin.core.dao.BaseDao;
import cn.sofwin.core.utils.JdbcUtils;
import cn.sofwin.web.blog.entity.Article;

import java.util.List;

/**
 * 文章数据访问层
 */
public class ArticleDao extends BaseDao<Article> {

    @Override
    public int insert(Article article) {
        String sql = "insert into article(user_id, title, summary, content, add_time) values(?,?,?,?,?)";
        return JdbcUtils.insert(
                sql,
                article.getUser().getId(),
                article.getTitle(),
                article.getSummary(),
                article.getContent(),
                article.getAddTime());
    }

    @Override
    public int deleteById(Integer id) {
        return JdbcUtils.update("delete from article where id = ?", id);
    }

    @Override
    public int update(Article article) {
        String sql = "update article set summary = ?, content = ? where id = ?";
        return JdbcUtils.update(
                sql,
                article.getSummary(),
                article.getContent(),
                article.getId());
    }

    @Override
    public Article get(Integer id) {
        //TODO 有坑
        return JdbcUtils.queryForObject(Article.class, "select * from article where id = ?", id);
    }

    @Override
    public List<Article> all() {
        //TODO 有坑
        return JdbcUtils.queryForList(Article.class, "select * from article");
    }
}
